package model;

import java.sql.ResultSet;
import java.sql.SQLException;

import database.Database;

public class Room {

	// REGION private fields
	private int ID;
	private String name;
	private String streetAddress;
	private int zip;
	private String city;
	private int capacity;
	
	/*
	 * REGION public constructors
	 */
	public Room(int ID) {
		setID(ID);
		
		load();
	}
	
	public Room(String name, String address, int zip, String city, int capacity){
		this.name = name;
		this.streetAddress = address;
		this.zip = zip;
		this.city = city;
		this.capacity = capacity;
		this.ID = 0;
	}
	
	// REGION public getters
	public int getID() {
		return ID;
	}
	
	public String getName() {
		return name;
	}
	
	public String getStreetAddress() {
		return streetAddress;
	}
	
	public int getZip() {
		return zip;
	}
	
	public String getCity() {
		return city;
	}
	
	public int getCapacity() {
		return capacity;
	}
	
	/*
	 *  REGION public setters
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	public void setStreetAddress(String streetAddress) {
		this.streetAddress = streetAddress;
	}
	
	public void setZip(int zip) {
		this.zip = zip;
	}
	
	public void setCity(String city) {
		this.city = city;
	}
	
	public void setCapacity(int capacity) {
		this.capacity = capacity;
	}
	
	/*
	 * REGION private setters
	 */
	private void setID(int ID) {
		this.ID = ID;
	}
	
	/*
	 * REGION public methods
	 */
	public void load() {
		Database database = new Database();
		database.connectToDatabase();
		
		try {
			ResultSet rs = database.getRoom(ID);
			
			if (rs.next()) {
				
				// Assign variables from the ResultSet to the object
				setName(rs.getString("name"));
				setStreetAddress(rs.getString("streetAddress"));
				setCity(rs.getString("city"));
				setZip(rs.getInt("zip"));
				setCapacity(rs.getInt("capacity"));
			}
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		database.disconnectDatabase();
	}
	
	public boolean isReserved(Meeting meeting) {
		
		//TODO IMPLEMENT
		return true;
	}
		
	public boolean hasCapacity(Meeting meeting) {
		if (meeting.getParticipantsCount() < capacity) {
			return true;
		}
		return false;
	}

	public void save(){
		Database db = new Database();
		db.connectToDatabase();
		
		
		if(this.ID!=0){
			db.updateRoom(this.ID, this.name, this.streetAddress, this.zip, this.city, this.capacity);
			
			}
		else{
			db.addRoom(this.name, this.streetAddress, this.zip, this.city, this.capacity);
			ResultSet rs = db.roomGetIdFromNameStreetAdress(this.name, this.streetAddress);
			try{
				if(rs.next()){
					this.ID = rs.getInt("id");
				}
				else{
					System.out.println("idoppdatering gikk ikke som det skulle");
				}
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		db.disconnectDatabase();
		
		
	}
	

	
	/*
	 * REGION override methods
	 */
	public String toString() {
		return ID + ";" + name + ";" + streetAddress + ";" + city + ";" + zip + capacity;
	}

}

